package com.iis.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.iis.databaseutil.DatabaseConnection;
import com.iis.entity.Orang;

public class OrangDAO {
	
	Connection koneksiKu = null;

	PreparedStatement psInsertOrang = null;

	String sqlInsertOrang = "INSERT INTO Orang(nama, tanggal_lahir, tempat_lahir) " + "VALUES (?,?,?) ";

	public boolean saveOrang(Orang newOrang) {
		if (newOrang == null) {
			return false;
		}
		koneksiKu = DatabaseConnection.getDataConnection();
		int index = 1;
		try {

			psInsertOrang = koneksiKu.prepareStatement(sqlInsertOrang);
			psInsertOrang.setString(index++, newOrang.getNama());
			// mengubah dari java.util.date ke java.sql.date
			java.sql.Date sqlBirthDate = new java.sql.Date(newOrang
					.getTanggalLahir().getTime());
			psInsertOrang.setDate(index++, sqlBirthDate);
			psInsertOrang.setString(index++, newOrang.getTempatLahir());
			int x = psInsertOrang.executeUpdate();
			if (x == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
